<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function countSort( arr ) {
            var newArr = [] ; 
            for( let i = 0 ; i < arr.length ; i ++ ) { 
                if( newArr[arr[i]] == undefined ) {
                    newArr[arr[i]] = 1 ; 
                } else {
                    newArr[arr[i]] ++ ;
                }
            }
            var newArr2 = [] ;
            for( let i = 0 ; i < newArr.length ; i ++ ){
                if ( newArr[i] !== undefined ) {
                    for( let j = 1 ; j <= newArr[i] ; j ++ ){
                        newArr2.push(i);
                    }
                }
            }
            return newArr2 ;     
        }
        var arr = [1,2,4,754,785,4,45,32,86,87,45,87,31,8,21,84,465,1867];
        console.log(countSort(arr));
        console.log(arr);
    </script>
</body>
</html>